from monai.data.image_reader import pydicom
import os

import zipfile
import csv




def  unzip(input_folder,output_folder):
    zip_files=[f for f in os.listdir(input_folder) if f.endswith('zip')]
    for zip_file in zip_files:
        zip_path=os.path.join(input_folder,zip_file)
        output=os.path.join(output_folder,zip_file)
        makedir_folder(output)

        with zipfile.ZipFile(zip_path,'r')  as zip_ref:
            zip_ref.extractall(output)


def is_empty(input_folder,csv_path):
    with open(csv_path, mode='w', newline='', encoding='utf-8') as file:
        write = csv.writer(file)
        write.writerow(['nrrd_name'])
        for dir_top in os.listdir(input_folder):
            dir_top_path = os.path.join(input_folder, dir_top)
            if os.path.isdir(dir_top_path):

                for dir_second in os.listdir(dir_top_path):
                    dir_second_path = os.path.join(dir_top_path, dir_second)
                    if os.path.isdir(dir_second_path):

                        for dir_third in os.listdir(dir_second_path):
                            dir_third_path = os.path.join(dir_second_path, dir_third)
                            if os.path.isdir(dir_third_path):

                                for dir_fourth in os.listdir(dir_third_path):
                                    dir_fourth_path = os.path.join(dir_third_path, dir_fourth)

                                    if os.path.isdir(dir_fourth_path):

                                        if len(os.listdir(dir_fourth_path)) < 5:
                                            print(len(os.listdir(dir_fourth_path)))
                                            write.writerow(dir_second+'_'+dir_fourth)



def dicom_series_to_nrrd(input_folder, csv_path):
    with open(csv_path, mode='w', newline='', encoding='utf-8') as file:
        write = csv.writer(file)
        write.writerow(['nrrd_name'])
        for dir_top in os.listdir(input_folder):
            dir_top_path = os.path.join(input_folder, dir_top)
            if os.path.isdir(dir_top_path):

                for dir_second in os.listdir(dir_top_path):
                    dir_second_path = os.path.join(dir_top_path, dir_second)
                    if os.path.isdir(dir_second_path):

                        for dir_third in os.listdir(dir_second_path):
                            dir_third_path = os.path.join(dir_second_path, dir_third)
                            if os.path.isdir(dir_third_path):

                                for dir_fourth in os.listdir(dir_third_path):
                                    dir_fourth_path = os.path.join(dir_third_path, dir_fourth)
                                    if os.path.isdir(dir_fourth_path):
                                        for file in os.listdir(dir_fourth_path):
                                            ID_number = os.path.basename(dir_second)
                                            file_path = os.path.join(dir_fourth_path, file)
                                            ds = pydicom.dcmread(file_path)
                                            #print(ds)
                                            patient_ID = ds.PatientID
                                            patient_name = ds.PatientName
                                            patient_birth = ds.PatientBirthDate
                                            if patient_ID == '' or patient_name == '' or patient_birth == '' or '/' in patient_ID or '/' in patient_name or '/' in patient_birth:
                                                write.writerow(ID_number)
                                            break



def makedir_folder(folder):
    if not os.path.exists(folder):
        os.makedirs(folder)



zip_folder = '/mnt/2093089d-5770-4686-b3f4-686eb07a909e/heart_1204_v1/heart/zip'
input_folder='/mnt/2093089d-5770-4686-b3f4-686eb07a909e/heart_1204_v1/heart/20250123'

problem_dicom_csv='/mnt/2093089d-5770-4686-b3f4-686eb07a909e/heart_1204_v1/heart/20250123result/problem_dicom_csv'
problem_information_csv = '/mnt/2093089d-5770-4686-b3f4-686eb07a909e/heart_1204_v1/heart/20250123result/problem_information_csv'


#unzip(zip_folder, input_folder)
is_empty(input_folder, problem_dicom_csv)
dicom_series_to_nrrd(input_folder, problem_information_csv)
